package br.skytef.stmanager.beans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import br.skytef.stmanager.daos.ClienteDAO;
import br.skytef.stmanager.daos.TarefaDAO;
import br.skytef.stmanager.entities.ClienteEntity;
import br.skytef.stmanager.entities.TarefaEntity;
import br.skytef.stmanager.utils.Consulta;


@ManagedBean(name = "ConsultaTarefaBean")
@SessionScoped
public class ConsultaTarefaBean {

	private int tpConsulta;
	public int getTpConsulta() {return tpConsulta;}
	public void setTpConsulta(int tpConsulta) {this.tpConsulta = tpConsulta;}

	private String textoConsulta;
	public String getTextoConsulta() {return textoConsulta;}
	public void setTextoConsulta(String textoConsulta) {this.textoConsulta = textoConsulta;}

	private TarefaEntity tarefa;
	public TarefaEntity getTarefa() {return tarefa;}
	public void setTarefa(TarefaEntity tarefa) {this.tarefa = tarefa;}

	public ClienteEntity getCliente() {return cliente;}
	public void setCliente(ClienteEntity cliente) {this.cliente = cliente;}
	private ClienteEntity cliente;

	public String getStatus() {return status;}
	public void setStatus(String status) {this.status = status;}
	private String status;

	public List<TarefaEntity> getListaTarefa() {return listaTarefa;}
	public void setListaTarefa(List<TarefaEntity> listaTarefa) {this.listaTarefa = listaTarefa;}
	private List<TarefaEntity> listaTarefa;
	
	public List<ClienteEntity> getAutocomplete() {return autocomplete;}
	public void setAutocomplete(List<ClienteEntity> autocomplete) {this.autocomplete = autocomplete;}
	public  List<ClienteEntity> autocomplete;
	
	public String getCnpjselecionado() {return cnpjselecionado;}
	public void setCnpjselecionado(String cnpjselecionado) {this.cnpjselecionado = cnpjselecionado;}
	private String cnpjselecionado;

	public ConsultaTarefaBean(){
		setTarefa(new TarefaEntity());
		setListaTarefa(null);
		setCliente(null);

	}

	public void consultarTarefa(){
		tarefa = new TarefaEntity();

		if ((tpConsulta == 1 || tpConsulta == 2 ||tpConsulta == 3) && (!status.equals("T"))){ 
			cliente = new Consulta().cliente(textoConsulta);
			listaTarefa = (new TarefaDAO().listar(cliente.getIdCliente(),null,null,status));

		}else if (tpConsulta == 4 && !status.equals("T")){
			listaTarefa = (new TarefaDAO().listar(0,textoConsulta,null,status));

		}else if (tpConsulta == 5 && !status.equals("T")){
			listaTarefa = (new TarefaDAO().listar(0,null,textoConsulta,status));

		}else if ((tpConsulta == 1 || tpConsulta == 2 ||tpConsulta == 3) && (status.equals("T"))){
			cliente = new Consulta().cliente(textoConsulta);
			listaTarefa = (new TarefaDAO().listarTodos(cliente.getIdCliente(), null, null));
			getListAutocomplete();

		}else if (tpConsulta == 4 && status.equals("T")){
			listaTarefa = (new TarefaDAO().listarTodos(0,textoConsulta,null));

		}else if (tpConsulta == 5 && status.equals("T")){
			listaTarefa = (new TarefaDAO().listarTodos(0,null,textoConsulta));

		}else if (tpConsulta == 0 && !status.equals("T")){
			listaTarefa = (new TarefaDAO().listarDoisStatus(status));

		}else if (tpConsulta == 0 && status.equals("T")){
			listaTarefa = (new TarefaDAO().listarTodosStatus(status));
		}
	}

	public void pageHistorico() throws IOException{
		new DashboardBean();
		DashboardBean.pageHistoricoTarefa();
		
		new HistoricoTarefaBean();
		HistoricoTarefaBean.carregar(tarefa);
	}

	public List <ClienteEntity> getListAutocomplete(){
		autocomplete = new ClienteDAO().listarCliente(textoConsulta);
		return autocomplete;
	}

	public List<ClienteEntity> complete() {    
		List<ClienteEntity> sugestoes = new ArrayList <ClienteEntity>();  

		List<ClienteEntity> results;  
		results = new ClienteDAO().listarCliente(textoConsulta);
		for(ClienteEntity r : results){  
			r.getCnpj();
			sugestoes.add(r);  
		}  

		return sugestoes;  
	}  
	
	public String refreshPagina(){
		listaTarefa = null;
		return null;
	}

}
